import headerWrap from "../commonComponents/header.vue"
import noDataWrap from "../commonComponents/noMoreData.vue"
import loadingWrap from "../commonComponents/loadingInPage.vue"
import switchTab from "../commonComponents/switchTab.vue"
import searchList from "../progressPublic/searchList.vue"
import voiceTxtArea from "../commonComponents/voiceTxtarea.vue"

let listType = [
  {text: '信息', value: 1},
  {text: '预警', value: 2},
  {text: '风险提示', value: 3},
  {text: '事件', value: 4},
  {text: '每日零报告', value: 5}
];

let holder = "点击选择类型  >";
let holder2 = "点击选择时间  >";

export default {
  data() {
    return {
      noDataWrapShow: false,
      type: 1,
      WrapShow: false,
      show: false,
      pageIndex: 0,
      pendingList: [],
      detailPath: null,
      listTypePicked: {text: holder, value: 1},
      beginTime: {text: holder2, value: ""},
      endTime: {text: holder2, value: ""},
      searchWords: "",
    }
  },
  methods: {
    init() {
      _methods.headerTitleChange(130);
      setTimeout(() => {
        this.pendingListInit();
      }, 200);
      // _methods.headerRightShow({
      //   val: 998,
      //   txt: ''
      // });
    },
    pendingListInit() {
      let self = this;
      _data.workReportList = _methods.initMeScroll("pending", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          warpId: "pending",
          callback: () => {
            self.getList(1, 2);
          }
        }
      });
    },
    //列表
    getList(listFlag, scrollFlag) {
      /*
       listFlag：1.待办 2.已办 3.办结
       scrollFlag：1：刷新 2：加载更多
       */
      let self = this;
      //列表参数
      let listKey = "pendingList";
      //scrollKey
      let scrollKey = "workReportList";
      //页码参数
      let pageIndex = "pageIndex";

      let data = {
        size: _data.pageSize,
        // condition: this.searchWords,
        // timeType: 2,
        // beginTime: this.beginTime.value,
        // endTime: this.endTime.value,
        // type: this.listTypePicked.value,
      };
      //ajax获取列表数据
      let ajData = (s, pg = 1) => {
        data.page = pg;
        _ajax.noLoading(_ajaxUrls.workReportList.url,
          data,
          (v) => {
            let val = self.listFormat(v.result.records);
            let len = val.length;
            if (!len) {
              this.noDataWrapShow = true;
            }
            if (s === 1) {
              //刷新
              self[listKey] = val;
            } else {
              //加载更多
              let arr = this[listKey];
              val.forEach((i) => {
                arr.push(i);
              });
              self[listKey] = arr;
            }
            _data[scrollKey].endBySize(len, _data.pageSize);

          }, _ajaxUrls.workReportList.type);
      };

      if (scrollFlag === 1) {
        //刷新列表
        self[pageIndex] = 1;
        ajData(scrollFlag, self[pageIndex]);
      } else {
        // 加载更多
        self[pageIndex] = parseInt(self[pageIndex]) + 1;
        ajData(scrollFlag, self[pageIndex]);
      }
    },
    go_details(item) {

      let procInstId = item.procInstId ? item.procInstId : "";

      let emergencyDeptId = item.deptId ? item.deptId : "";

      // this.type：1信息 2预警 3风险提示 4事件 5每日零报告


      // listType 1事件

      let flag = parseInt(this.listTypePicked.value);

      let path = {
        1: `/workReport/infoDetail?id=${item.businessId}&type=2&isWorkReport=1`,
        2: `/workReport/detail?procInstId=${procInstId}&id=${item.businessId}&emergencyDeptId=${emergencyDeptId}&listType=4&type=3&flag=1`,
        3: `/workReport/detail?procInstId=${procInstId}&id=${item.businessId}&emergencyDeptId=${emergencyDeptId}&listType=4&type=3&flag=1`,
        4: `/workReport/detail?procInstId=${procInstId}&id=${item.businessId}&emergencyDeptId=${emergencyDeptId}&listType=4&type=2&flag=1`,
        5: `/workReport/drDetail?id=${item.businessId}&isPending=0&listType=1&state=-1`,
      }[flag];

      _methods.goPath(path);
    },
    listFormat(arr) {
      // this.type：1信息2预警3风险提示4事件5每日零报告
      let type = {
        1: "信息",
        2: "预警",
        3: "风险提示",
        4: "事件",
        5: "每日零报告",
      };
      return arr.map((i) => {
        i.typeTxt = type[this.type - 0];
        i.time = i.createTime;
        return i;
      });
    },
    searchShow(val = true) {
      this.WrapShow = val;
      setTimeout(() => {
        this.show = val;
      }, 100);
    },
    typePick() {
      let self = this;
      let picker = new mui.PopPicker();
      picker.setData(listType);
      picker.show(function (selectItems) {
        self.listTypePicked = selectItems[0];
      })
    },
    inputValue(v) {
      let value = v.target.value;
      this.searchWords = value;
    },
    _timePick(type) {

      let self = this;

      let dtPicker = new mui.DtPicker({type: "date"});

      dtPicker.show(function (selectItems) {
        let vl = selectItems.value;
        if (type === 1) {
          self.beginTime.text = vl;
          self.beginTime.value = vl;
        } else {
          self.endTime.text = vl;
          self.endTime.value = vl;
        }
      })

    },
    searchSubmit() {
      _methods.meScrollTriggerDownScroll(_data.workReportList);
      this.searchShow(false);
    },
    vSearch(flag,ele) {
      let el;
      if (flag === 1) {
        el = ele.target.querySelectorAll(".icon-voice")[0];
      }
      if (flag === 2) {
        el = ele.target.nextElementSibling.querySelectorAll(".icon-voice")[0];
      }

      el.click();
    }
  },
  computed: {
    infoType() {
      // {text: '信息', value: 1},
      // {text: '预警', value: 2},
      // {text: '风险提示', value: 3},
      // {text: '事件', value: 4},
      // {text: '每日零报告', value: 5}

      let listType = {
        1: "信息",
        2: "预警",
        3: "风险提示",
        4: "事件",
        5: "每日零报告",
      }[this.listTypePicked.value];

      return listType;
    },
  },
  components: {
    headerWrap,
    noDataWrap,
    loadingWrap,
    switchTab,
    searchList,
    voiceTxtArea,
  },
  mounted() {
    this.init();
    _data.busInit.on_(() => {
      this.searchShow(!this.WrapShow);
    });
    _data.bus.$on("searchVoice", (words) => {
      mui.toast("开始搜索" + words);
      this.searchWords = words;
      this.searchSubmit();
      setTimeout(() => {
        this.searchWords = "";
      }, 200);
    })
  },
  beforeDestroy() {
    _methods.headerRightShow();
    _data.busInit.off_();
    _data.bus.$off("searchVoice");
  }
}
